
#include "tst.hpp"
#include "xen_mmc_mng.hpp"

#include "HsimIncl.h"
#include "xsg_sp_cmd.hpp"


void init_agent()
{
    io_service  ioserv;
    cmd_agent_mng_ptr    cmdmng = cmd_agent_mng::instance();
    xen_mmc_mng_ptr	    mmcmng = xen_mmc_mng::instance();
    vty_agent_server agent(ioserv, 8132);

    // Block all signals for background thread.
    sigset_t new_mask;
    sigfillset(&new_mask);
    sigset_t old_mask;
    pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);

    tcp::endpoint endpoint(tcp::v4(), 8132);

    boost::thread cmdmngthr(boost::bind(&cmd_agent_mng::run, cmdmng));
    boost::thread agentthr(boost::bind(&vty_agent_server::run, &agent));

    pthread_sigmask(SIG_SETMASK, &old_mask, 0);

    // Wait for signal indicating time to shut down.
    sigset_t wait_mask;
    sigemptyset(&wait_mask);
    sigaddset(&wait_mask, SIGINT);
    sigaddset(&wait_mask, SIGQUIT);
    sigaddset(&wait_mask, SIGTERM);
    pthread_sigmask(SIG_BLOCK, &wait_mask, 0);

	build_xsg_sp_cmd(cmdmng);
#if 0
    build(cmdmng);
	build_exec(cmdmng);
#endif

    int sig = 0;
    sigwait(&wait_mask, &sig);

    cmdmng->stop();
    agent.stop();
    cmdmngthr.join();
    agentthr.join();

    return ;
}

